package com.ilop.sthome.ui.activity.scene;

import android.annotation.SuppressLint;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.MotionEvent;
import android.view.View;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.mvp.contract.scene.SetActionContract;
import com.ilop.sthome.mvp.present.scene.SetActionPresenter;
import com.ilop.sthome.ui.adapter.scene.ChoseActionAdapter;
import com.example.common.view.WheelView;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivitySetActionBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2020-02-02.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设置输入输出动作（触发条件和执行动作）
 */
public class SettingActionActivity extends ContainLoginActivity<SetActionPresenter, ActivitySetActionBinding> implements SetActionContract.IView {

    private boolean isAdd;
    private boolean isInput;
    private boolean isUpdate;
    private boolean isLock;
    private DeviceInfoBean mDevice;
    private AutomationBean mAutoBean;
    private ChoseActionAdapter mAdapter;

    private List<String> mTrigger;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_action;
    }

    @Override
    protected void initialize() {
        super.initialize();
        isInput = getIntent().getBooleanExtra(CommonId.KEY_INPUT, false);
        isUpdate = getIntent().getBooleanExtra(CommonId.KEY_UPDATE, false);
        mDevice = (DeviceInfoBean) getIntent().getSerializableExtra(CommonId.KEY_DEVICE);
        isAdd = getIntent().getBooleanExtra(CommonId.KEY_ADD_NEW, false);
        mAutoBean = (AutomationBean) getIntent().getSerializableExtra(CommonId.KEY_AUTOMATION);
        isLock = getIntent().getBooleanExtra(CommonId.KEY_MESSAGE, false);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new SetActionPresenter(mContext);
    }

    @Override
    protected void initView() {
        super.initView();
        mPresent.getDeviceInfo(mDevice);
        String timer_dw = "(" + getString(R.string.m_s) + ")";
        mDBind.setDelayTitle.setText(timer_dw);
        mDBind.tvSetAction.setText(isInput ? getString(R.string.please_select_trigger_condition) : getString(R.string.please_select_the_execution_action));
        mDBind.actionIsOutput.setVisibility(isInput ? View.GONE : View.VISIBLE);
        mDBind.actionMinute.setCyclic(true);
        mDBind.actionSecond.setCyclic(true);
        mDBind.actionTrigger.setLayoutManager(new LinearLayoutManager(mContext));
        if (isUpdate) mDBind.actionDelete.setVisibility(View.VISIBLE);
        mPresent.getMinuteAndSecond();
    }

    @Override
    protected void initData() {
        super.initData();
        mPresent.getTrigger(isLock);

        LiveDataBus.get().with(CommonId.CHOOSE_ACTION, Integer.class).observe(this , integer -> mAdapter.setPosition(integer));

        if (isUpdate){ mPresent.showUpdateData();}
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initListener() {
        super.initListener();
        mDBind.tvSaveAction.setOnClickListener(v -> {
            if (mAdapter.getPosition() == -1){
                showToast(getString(R.string.please_select_the_execution_action));
                return;
            }
            if (isInput) {
                String action = mTrigger.get(mAdapter.getPosition());
                mPresent.editInputDeviceInfo(action);
                if (isAdd){
                    mPresent.isAddNewInputAction();
                }else {
                    mPresent.isAddOrUpdateInput(isUpdate);
                }
            } else {
                int minute = mDBind.actionMinute.getCurrentItem();
                int second = mDBind.actionSecond.getCurrentItem();
                String action = mTrigger.get(mAdapter.getPosition());
                mPresent.editOutputDeviceInfo(minute, second, action);
                if (isAdd){
                    mPresent.isAddNewOutputAction(mAutoBean);
                }else {
                    mPresent.isAddOrUpdateOutput(isUpdate);
                }
            }
            finish();
        });

        mDBind.actionDelete.setOnClickListener(view -> {
            LiveDataBus.get().with(CommonId.DELETE_CONDITION).setValue(isInput);
            finish();
        });
        mDBind.ivBack.setOnClickListener(v -> finish());

        mDBind.actionMinute.setOnTouchListener((view, motionEvent) -> {
            if (motionEvent.getAction() == MotionEvent.ACTION_UP){
                mDBind.actionScroll.requestDisallowInterceptTouchEvent(false);
            }else {
                mDBind.actionScroll.requestDisallowInterceptTouchEvent(true);
            }
            return false;
        });
        mDBind.actionSecond.setOnTouchListener((view, motionEvent) -> {
            if (motionEvent.getAction() == MotionEvent.ACTION_UP){
                mDBind.actionScroll.requestDisallowInterceptTouchEvent(false);
            }else {
                mDBind.actionScroll.requestDisallowInterceptTouchEvent(true);
            }
            return false;
        });
    }

    @Override
    public void showTitle(String msg) {
        mDBind.tvSetTitle.setText(msg);
    }

    @Override
    public void showTime(int minute, int second) {
        mDBind.actionMinute.setCurrentItem(minute);
        mDBind.actionSecond.setCurrentItem(second);
    }

    @Override
    public void showAction(int position) {
        mAdapter.setPosition(position);
    }

    @Override
    public void showTrigger(ArrayList<String> trigger) {
        mAdapter = new ChoseActionAdapter(mContext, trigger);
        mAdapter.setPosition(0);
        mDBind.actionTrigger.setAdapter(mAdapter);
    }

    @Override
    public void showTriggerCode(List<String> triggerCode) {
        this.mTrigger = triggerCode;
    }

    @Override
    public void showMinuteAndSecond(ArrayList<String> number) {
        mDBind.actionMinute.setAdapter(new NumberAdapter(number));
        mDBind.actionSecond.setAdapter(new NumberAdapter(number));
    }

    private static class NumberAdapter extends WheelView.WheelArrayAdapter<String> {
        NumberAdapter(ArrayList<String> items) {
            super(items);
        }
    }
}
